package api

import (
	"api_gateway/basic/config"
	chat "api_gateway/basic/proto/chat"
	"api_gateway/handler/request/chat"
	"api_gateway/handler/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 发送消息
func SendMessage(c *gin.Context) {
	// 接收参数
	var req request.SendMessageReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}

	// 调用服务端
	resp, err := config.ChatClient.SendMessage(c, &chat.SendMessageReq{
		FormUserId:  int64(req.FormUserId),
		ToTargetId:  int64(req.ToTargetId),
		Content:     req.Content,
		MessageType: req.MessageType,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 获取会话消息
func GetConversation(c *gin.Context) {
	// 接受参数
	var req request.GetMessageReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务端
	resp, err := config.ChatClient.GetMessage(c, &chat.GetMessageReq{
		ConversationId: strconv.Itoa(int(req.ConversationId)),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 创建群聊
func CreateGroup(c *gin.Context) {
	// 接收参数
	var req request.CreateGroupReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}

	// 调用服务端
	group, err := config.ChatClient.CreateGroupMessage(c, &chat.CreateGroupMessageReq{
		GroupName:    req.GroupName,
		GroupAvatar:  req.GroupAvatar,
		Announcement: req.Announcement,
		OwnerName:    req.OwnerName,
		OwnerId:      int64(req.OwnerId),
		MaxMembers:   int64(req.MaxMembers),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, group)
}

// 加入群聊
func JoinGroup(c *gin.Context) {
	// 接收参数
	var req request.JoinGroupReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务端
	resp, err := config.ChatClient.JoinGroup(c, &chat.JoinGroupReq{
		GroupId:        req.GroupId,
		UserId:         int64(req.UserId),
		MemberNickname: req.MemberNickname,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 发送群消息
func SendGroupMessage(c *gin.Context) {
	// 接收参数
	var req request.SendMessageReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务端
	resp, err := config.ChatClient.SendGroupMessage(c, &chat.SendGroupMessageReq{
		GroupId:     req.Content,
		SenderId:    req.MessageType,
		Content:     req.Content,
		MessageType: req.MessageType,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 获取群消息记录
func GetGroupMessage(c *gin.Context) {
	// 接收参数
	var req request.GetGroupMessageReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetGroupMessage(c, &chat.GetGroupMessageReq{
		GroupId: req.GroupId,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 获取群成员
func GetGroupMember(c *gin.Context) {
	// 接收参数
	var req request.GetGroupMemberReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetGroupMember(c, &chat.GetGroupMemberReq{
		GroupId: req.GroupId,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 查询用户所在群聊
func GetUserGroups(c *gin.Context) {
	// 接收参数
	var req request.GetUserGroupsReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetUserGroups(c, &chat.GetUserGroupsReq{
		UserId: int64(req.UserId),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 获取随机问题
func GetRandomQuestion(c *gin.Context) {
	// 接收参数
	var req request.GetRandomQuestionReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetRandomQuestion(c, &chat.GetRandomQuestionReq{})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 按分类/兴趣获取随机问题
func GetRandomQuestionByCategory(c *gin.Context) {
	// 接收参数
	var req request.GetRandomQuestionByCategoryReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetRandomQuestionByCategory(c, &chat.GetRandomQuestionByCategoryReq{
		Category: req.Category,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}

// 表情包推荐
func GetRandomStickerByCategory(c *gin.Context) {
	// 接收参数
	var req request.GetRandomStickerByCategoryReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.GetRandomStickerByCategory(c, &chat.GetRandomStickerByCategoryReq{
		Context: req.Context,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}
	response.Success(c, resp)
}
